package com.hsy.linemoney.modules.auth.activity;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.util.TypedValue;
import android.view.ViewGroup;
import android.widget.TextView;

import com.deparse.dpframe.tools.utils.DPUnitUtil;
import com.deparse.dpframe.ui.android.app.activity.DPBaseActivity;
import com.deparse.dpframe.ui.android.widget.DPViewPager;
import com.deparse.dpframe.ui.android.widget.DPagerSlidingTabStrip;
import com.hsy.linemoney.R;
import com.hsy.linemoney.modules.auth.fragment.SignInFragment;
import com.hsy.linemoney.modules.auth.fragment.SignUpFragment;
import com.hsy.linemoney.modules.common.CommonBaseActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/7/6.
 */
public class LoginActivity extends CommonBaseActivity {

    private DPViewPager activity_repair_viewpager;
    private List<Fragment> myFragmentList;
    private DPagerSlidingTabStrip tabs;
    private MyPagerAdapter adapter;

    private DPViewPager.OnPageChangeListener onPageChangeListener = new DPViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int i, float v, int i1) {

        }

        @Override
        public void onPageSelected(int i) {
            if (i == 0) {
                ((TextView) ((ViewGroup) tabs.getChildAt(0)).getChildAt(0)).setTextAppearance(LoginActivity.this, R.style.login_pagerSlidingTab_text_true);
                ((TextView) ((ViewGroup) tabs.getChildAt(0)).getChildAt(1)).setTextAppearance(LoginActivity.this, R.style.login_pagerSlidingTab_text_false);
            } else {
                ((TextView) ((ViewGroup) tabs.getChildAt(0)).getChildAt(0)).setTextAppearance(LoginActivity.this, R.style.login_pagerSlidingTab_text_false);
                ((TextView) ((ViewGroup) tabs.getChildAt(0)).getChildAt(1)).setTextAppearance(LoginActivity.this, R.style.login_pagerSlidingTab_text_true);
            }
        }

        @Override
        public void onPageScrollStateChanged(int i) {

        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setStausBarBackgroundColor(getResources().getColor(R.color.color_action_bar_bg));

        setContentView(R.layout.activity_login);

        activity_repair_viewpager = F(R.id.activity_repair_viewpager);


        tabs = F(R.id.tabs);


        tabs.setOnPageChangeListener(onPageChangeListener);


        showTab();

    }

    private void showTab() {
        myFragmentList = new ArrayList<Fragment>();
        SignInFragment signInFragment = new SignInFragment();
        SignUpFragment signUpFragment = new SignUpFragment();
        signInFragment.setAttchedActivity(this);
        signUpFragment.setAttchedActivity(this);
        myFragmentList.add(signInFragment);
        myFragmentList.add(signUpFragment);

        adapter = new MyPagerAdapter(getSupportFragmentManager(), myFragmentList);


        tabs.setIndicatorWidth(DPUnitUtil.dip2px(this, 50));


        int pageMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 4, getResources().getDisplayMetrics());
        activity_repair_viewpager.setAdapter(adapter);
        activity_repair_viewpager.setPageMargin(pageMargin);
        tabs.setViewPager(activity_repair_viewpager);

        onPageChangeListener.onPageSelected(0);
    }

    private class MyPagerAdapter extends FragmentPagerAdapter {

        private final String[] TITLES = {getResources().getString(R.string.login_sign_in_title), getResources().getString(R.string.login_sign_up_title)};
        private List<Fragment> myFragmentsList;

        public MyPagerAdapter(FragmentManager fragmentmanager, List<Fragment> list) {
            super(fragmentmanager);
            myFragmentsList = list;
        }

        public int getItemPosition(Object obj) {
            return super.getItemPosition(obj);
        }

        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        public int getCount() {
            return myFragmentsList.size();
        }

        public Fragment getItem(int i) {
            return (Fragment) myFragmentsList.get(i);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return TITLES[position];
        }

    }
}
